#include "groupchat.h"
#include "ui_groupchat.h"
#include"QTableView"
#include <QStandardItemModel>
#include<QAbstractItemView>
#include"QPushButton"
groupchat::groupchat(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::groupchat)
{
    ui->setupUi(this);
}

groupchat::~groupchat()
{
    delete ui;
}
void groupchat::getinfo(QMap<QString ,QString> map){
   groupNameAndId=map;
   groupmyfriendnum=groupNameAndId.size();
       // grouptableView = new QTableView(ui->tableView);
         grouptableView->setMinimumSize(700, 500);
         grouptableView->verticalHeader()->hide(); // hide row number
        //grouptableModel = new QStandardItemModel(ui->tableView);
         grouptableView->setModel(grouptableModel);
         // set columns
         QStringList columnTitles;
         columnTitles << "好友序号" << "好友id" << "好友昵称" << "通讯";
         grouptableModel->setHorizontalHeaderLabels(columnTitles);
     //    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // column fit the contents

         // add contents

               QMap<QString,QString>::Iterator it = groupNameAndId.begin();
              groupmyfriendnum=0;
                 while(it!=groupNameAndId.end()){
                    grouptableModel->setItem(groupmyfriendnum, 0, new QStandardItem(QString::number( groupmyfriendnum + 1)));
                    grouptableModel->setItem(groupmyfriendnum, 1, new QStandardItem(QString( it.key()).arg( groupmyfriendnum)));
                   grouptableModel->setItem(groupmyfriendnum, 2, new QStandardItem( it.value()));
                      // add button to the last column
                      QPushButton *chatbutton = new  QPushButton("开始聊天");
                    // chatbutton->id= it.key();
                      // set custom property
                      chatbutton->setProperty("序号", groupmyfriendnum); // set custom property
                      chatbutton->setProperty("id", it.key());
                      chatbutton->setProperty("昵称",  it.value());
                      // set click event
                    connect(chatbutton, SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked));
                      // notice every time insert the button at the last line
                      grouptableView->setIndexWidget(grouptableModel->index(grouptableModel->rowCount() - 1, 3), chatbutton);
           groupmyfriendnum++;
                     it++;}
}
void groupchat::getgrouphistory(QList<QPair<QString,QString>> list){
 grouphistory=list;
QString name;
 //Account::getInstance()->getA_Uid_GroupName(history[i].first,name);
 for(int i=0;i<grouphistory.size();i++){
    qDebug() << name;
     Account::getInstance()->getA_Uid_GroupFriendName(grouphistory[i].first,name);
    qDebug()<<"list ";
     ui->textBrowser->append(name+": "+grouphistory[i].second);

 }

}

void groupchat::on_pushButton_clicked()
{
QString name;
        ui->textBrowser->setAlignment(Qt::AlignRight);
          /*ui->textBrowser->setStyleSheet(" color:rgb(55,100,255)");
           QDateTime time = QDateTime::currentDateTime();
           QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
           ui->textBrowser->append(str);*/

        Account::getInstance()->getA_Uid_GroupFriendName(myid,name);
          ui->textBrowser->append(name+": "+ui->sendtext->toPlainText());
          emit(sendGroupMsg(groupid,myid,ui->sendtext->toPlainText()));
           ui->sendtext->clear();

}
void groupchat::GetGroupInfo(QString gid,QString uid,QString info){



    QString name;
    Account::getInstance()->getA_Uid_GroupFriendName(uid,name);
 ui->textBrowser->append(name+": "+info);



}

void groupchat::on_pushButton_2_clicked()
{
    emit ( addfriends(ui->lineEdit->text()));
}
